home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / clang / c1.zip / LIFE.C < prev    next >
Text File  |  1987-06-18  |  9KB  |  247 lines

  1.  
  2. /*      LIFE.C          The much implemented game of Life invented by John Conway
  3.  
  4.                                 This version was written to illustrate the use of the C88
  5.                                 screen and keyboard interface.
  6.  
  7.                                 To generate:
  8.                                 A>C88 B:LIFE
  9.                                 A>ASM88 B:PCIO
  10.                                 A>BIND B:LIFE B:PCIO            */
  11.  
  12.  
  13. /*
  14.   global constant and data declarations
  15. */
  16.  
  17. #define ROWS    24
  18. #define COLS    80
  19.  
  20. /* control key translations */
  21. #define up_char 30
  22. #define down_char       31
  23. #define left_char 29
  24. #define right_char 28
  25. #define bol_char 200
  26. #define eol_char 201
  27. #define pageup_char 202
  28. #define pagedown_char 203
  29. #define bof_char 204
  30. #define eof_char 205
  31. #define Ins_char 206
  32. #define Del_char 207
  33. #define NextWord_char 208
  34. #define PrevWord_char 209
  35.  
  36. /* function keys */
  37. #define M1 210
  38. #define M2 211
  39. #define M3 212
  40. #define M4 213
  41. #define M5 214
  42. #define M6 215
  43. #define M7 216
  44. #define M8 217
  45. #define M9 218
  46. #define M10 219
  47.  
  48.  
  49. char world[ROWS][COLS],create_mode=1,quit_flag;
  50. int  population,generation,crow,ccol;
  51.  
  52. main() {
  53.  
  54.         scr_setup();
  55.         scr_clr();
  56.         instruct();
  57.         setup();
  58.  
  59.         do {
  60.                 generation++;
  61.                 cycle();
  62.                 screen();
  63.                 }
  64.         while (population && !quit_flag);
  65.         scr_rowcol(ROWS,10);
  66.         if (population == 0)
  67.                 puts("Nobody left, sorry about that.                       ");
  68.         else puts("bye                                                 ");
  69.         scr_curson();
  70.         }
  71.  
  72. instruct() {            /*      print instructions      */
  73.  
  74. puts("                The game of Life by John Conway\n\n");
  75. puts("      If started with a number, a random pattern starts the game.\n\n");
  76. puts("  Otherwise, move the cursor with the four arrow keys to create life.\n\n");
  77. puts("     DEL changes cursor movement to mean that cells are deleted\n\n");
  78. puts("                 INS flips back to create mode.\n\n");
  79. puts("          The '+' key will toggle the game on or off.\n\n");
  80. puts("                     Hit ESC to bail out.\n\n");
  81. puts("            Enter starting number of cells or hit CR   ");
  82.         }
  83.  
  84. setup() {
  85.         int rnumber;
  86.         int i,row,col,seed,rnum;
  87.         char ch;
  88.  
  89.         rnumber=0;
  90.         while (1) {
  91.                 while ((ch=scr_csts()) == 0) seed++;
  92.                 if (ch < '0' || ch > '9') break;
  93.                 scr_co(ch);
  94.                 rnumber*=10;
  95.                 rnumber+=ch-'0';
  96.                 }
  97.         scr_cursoff();
  98.         scr_clr();
  99.         scr_rowcol(ROWS,20);    /*      print population message        */
  100.         puts("Generation    0     Population    0");
  101.  
  102.         srand(seed);            /*      initilize random number generator       */
  103.  
  104.         for (i=0; i < rnumber; i++) {
  105.                 rnum=rand();
  106.                 row=rnum%ROWS;
  107.                 col=(rnum/ROWS)%COLS;
  108.                 world[row][col]='X'; /* put in a cell */
  109.                 scr_rowcol(row,col);
  110.                 scr_co(2);
  111.                 }
  112.         if (rnumber == 0) create(1);
  113.         }
  114.  
  115.  
  116. screen() {      /* update the screen and set world back to x's  */
  117.         int  row,col;
  118.         char cell;
  119.  
  120.         population=0;
  121.         for (row=0; row < ROWS; row++) {
  122.                 for (col=0; col < COLS; col++) {
  123.                         cell=world[row][col];
  124.                         /* stay alive if 3 neighbors, born if next to 2 or 3 */
  125.  
  126.                         if (cell && (cell == 3 || cell == 'X'+2 || cell == 'X'+3)) {
  127.                                 population++;
  128.                                 if (cell < 'X') {
  129.                                         scr_rowcol(row,col);
  130.                                         scr_co(2);
  131.                                         }
  132.                                 cell='X';
  133.                                 }
  134.                         else {
  135.                                 if (cell >= 'X') {
  136.                                         scr_rowcol(row,col);
  137.                                         scr_co(' ');
  138.                                         }
  139.                                 cell=0;
  140.                                 }
  141.                         world[row][col]=cell;
  142.                         }
  143.                 }
  144.         scr_rowcol(ROWS,31);
  145.         printf("%4d",generation);
  146.         scr_rowcol(ROWS,51);
  147.         printf("%4d",population);
  148.         }
  149.  
  150.  
  151. create(suspend)                 /*      see if need to create or kill cells     */
  152.         char suspend; {
  153.         char ch,wait;
  154.  
  155.         while ((ch=scr_csts()) || suspend) {
  156.                 switch (ch) {
  157.                         case up_char:   crow=crow ? crow-1: ROWS-1;
  158.                                                         break;
  159.                         case down_char: crow=crow == ROWS-1 ? 0: crow+1;
  160.                                                         break;
  161.                         case left_char: ccol=ccol ? ccol-1: COLS-1;
  162.                                                         break;
  163.                         case right_char:ccol=ccol == COLS-1 ? 0: ccol+1;
  164.                                                         break;
  165.                         case bol_char:  ccol=0;
  166.                                                         break;
  167.                         case eol_char:  ccol=COLS-1;
  168.                                                         break;
  169.                         case '+':               suspend=!suspend;
  170.                                                         continue;
  171.                         case Ins_char:  create_mode=1;
  172.                                                         continue;
  173.                         case Del_char:  create_mode=0;
  174.                                                         continue;
  175.                         case 0x1b:              quit_flag=1;    /* flag for stop */
  176.                                                         return;
  177.                         default:                continue;
  178.                         }
  179.                 world[crow][ccol]= create_mode ? 'X': 0;
  180.                 scr_rowcol(crow,ccol);
  181.                 if (create_mode) {
  182.                         scr_co(2);
  183.                         population++;
  184.                         }
  185.                 else {
  186.                         wait=30;
  187.                         while (wait--) {
  188.                                 scr_co(1);
  189.                                 scr_rowcol(crow,ccol);
  190.                                 }
  191.                         scr_co(' ');
  192.                         }
  193.                 }
  194.         }
  195.  
  196. cycle() {                               /* cycle to the next generation */
  197.         int  row,col;
  198.  
  199.         create(0);
  200.         /*      take care of left and right column first        */
  201.         for (row=0; row < ROWS; row++) {
  202.                 if (world[row][0] >= 'X') add8(row,0);
  203.                 if (world[row][COLS-1] >= 'X') add8(row,COLS-1);
  204.                 }
  205.  
  206.         /*      take care of top and bottom line        */
  207.         for (col=1; col < COLS-1;col++) {
  208.                 if (world[0][col] >= 'X') add8(0,col);
  209.                 if (world[ROWS-1][col] >= 'X') add8(ROWS-1,col);
  210.                 }
  211.  
  212.         /*      fill in the box, ignoring border conditions     */
  213.         for (row=1; row < ROWS-1; row++) {
  214.                 for (col=1; col < COLS-1; col++) {
  215.                         if (world[row][col] >= 'X' ) {
  216.                                 world[row-1][col-1]++;
  217.                                 world[row-1][col]++;
  218.                                 world[row-1][col+1]++;
  219.                                 world[row][col-1]++;
  220.                                 world[row][col+1]++;
  221.                                 world[row+1][col-1]++;
  222.                                 world[row+1][col]++;
  223.                                 world[row+1][col+1]++;
  224.                                 }
  225.                         }
  226.                 }
  227.         }
  228.  
  229.  
  230. add8(row,col)
  231.         int  row,col; {
  232.         int  rrow,ccol,rr,cc;
  233.  
  234.         for (rr=row-1; rr <= row+1; rr++) {
  235.                 for (cc=col-1; cc <= col+1; cc++) {
  236.                         rrow=rr != -1 ? rr : ROWS-1;
  237.                         ccol=cc != -1 ? cc : COLS-1;
  238.                         if (rrow >= ROWS) rrow=0;
  239.                         if (ccol >= COLS) ccol=0;
  240.                         world[rrow][ccol]++;
  241.                         }
  242.                 }
  243.         world[row][col]--;
  244.         }
  245.  
  246.  
  247.